package com.loadmore.common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import com.loadmore.common.R
import com.loadmore.common.base.impl.ViewImpl
import com.loadmore.common.model.MessageEvent
import com.loadmore.common.widget.StatusLayout
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 *@author sunleilei
 *@date 2018/7/12
 *@time 8:01
 *
 */
abstract class BaseFragment : Fragment(), StatusLayout.StatusListener, OnRefreshListener, OnLoadMoreListener,ViewImpl {
    lateinit var statusLayout: StatusLayout
    lateinit var actionBar: Toolbar
    var showNavigationIcon = true
    lateinit var refreshLayout:SmartRefreshLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (arguments != null) {
            showNavigationIcon =   arguments!!.getBoolean("showNavigationIcon",true)
        }
        var content = layoutInflater.inflate(R.layout.fragment_base,container,false)
        statusLayout = content.findViewById(R.id.status_layout)
        refreshLayout = content.findViewById(R.id.refreshLayout)
        refreshLayout.setEnableLoadMore(false)
        refreshLayout.setEnableRefresh(false)
        refreshLayout.setOnRefreshListener(this)
        refreshLayout.setOnLoadMoreListener(this)
        actionBar = content.findViewById(R.id.actionBar)
        actionBar.setNavigationOnClickListener {
            activity?.finish()
        }
        if (showNavigationIcon) {
            actionBar.setNavigationIcon(R.mipmap.icon_back)
            actionBar.navigationContentDescription = "返回"
        }
        var contentContainer:ViewGroup = content.findViewById(R.id.content_container)
        var contentView = layoutInflater.inflate(getLayoutId(),null)
        contentContainer.addView(contentView, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT))
        statusLayout.setStatusListener(this)
        return content
    }

    public fun setMenu(menu:Int) {
        actionBar.inflateMenu(menu)
        actionBar.setOnMenuItemClickListener {
            onMenuItemClick(it)
            return@setOnMenuItemClickListener true
        }
    }

    open fun onMenuItemClick(menuItem: MenuItem) {

    }
    public override fun showLoading() {
        statusLayout.showLoading()
    }

    public override fun showContent() {
        statusLayout.showContent()
    }

    public override fun showNetError() {
        statusLayout.showNetError()
    }

    public override fun showFailed() {
        statusLayout.showFailed()
    }

    public override fun showEmpty() {
        statusLayout.showEmpty()
    }

    public override fun finishRefresh() {
        refreshLayout.finishRefresh(true)
    }

    public override fun finishLoadMore() {
        refreshLayout.finishLoadMore()
    }

    public override fun setNoMoreData(noMoreData: Boolean) {
        refreshLayout.setNoMoreData(noMoreData)
    }

    public fun setNavigationIcon(boolean: Boolean) {
        this.showNavigationIcon = boolean
    }
    public fun setRefreshEnable(boolean: Boolean) {
        refreshLayout.setEnableRefresh(boolean)
    }
    public fun setLoadEnable(boolean: Boolean) {
        refreshLayout.setEnableLoadMore(boolean)
    }
    public fun hideNavigationIcon() {
        showNavigationIcon = false
    }

    public fun hideActionBar() {
        actionBar.visibility = View.GONE
    }

    public fun actionBarTitle(title:String) {
        //actionBar.setCenterTitle(title)
    }
    abstract fun getLayoutId():Int

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onMessageEvent(event: MessageEvent) {/* Do something */
    }

    override fun retry() {
    }


    override fun onLoadMore(refreshLayout: RefreshLayout) {
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
    }


    override fun netError() {
    }

    open fun finish() {
        activity?.finish()
    }
    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}